﻿/*
* 培训任务列表
* 
*/
var kdManagement = angular.module("questionnairelistModel", ['tm.pagination', 'btnconqitk', 'Common', 'interlocutionService', "loadModule", "editquestionnaire"])
.controller("questionnairelistController", ["$scope", "$rootScope", "sysUserInfo", "interlocutionservice", "Comm", "Msg", "MsgCon", function ($scope, $rootScope, sysUserInfo, IS, Comm, Msg, MsgCon) {
    $scope.btnlist = { savefun: false, addinfo: false, editinfo: false, delinfo: false };
    $rootScope.btnTab = "index.honor.questionnaire";
    $rootScope.secparstr = "index.honor.questionnaire";
    //初始化
    $scope.init = function () {
        //培训任务列表集合
        $scope.List = [];
        // 选中个数
        $scope.checkAll = 0;
        //查询培训任务列表参数
        $scope.params = {
            userid: sysUserInfo.getuserinfo().user_ID,  //登录人id
            searchText: "",    //搜索内容、
            pageIndex: 1,   //起始下标
            pageSize: 10 //页数
        }
        $scope.getList();
    }
    $scope.openUrl = "";
    //新增按钮
    $scope.openurl = function () {
        $scope.openUrl = "/system/views/wenjuan/wenjuan.html";
        $scope.openA();
    }
    $scope.openA = function () {
        // var a = document.getElementById("openUrl");
        window.open($scope.openUrl);
    }
    //全选
    $scope.selectAll = function () {
        $scope.selFlag = true;
        if ($scope.checkAll == $scope.List.length) { //已选
            $scope.selFlag = false;
            $scope.checkAll = 0;
        } else {
            $scope.selFlag = true;
            $scope.checkAll = $scope.List.length;
        }
        // 设置样式
        angular.forEach($scope.List, function (data, index, array) {
            data.isSel = $scope.selFlag;
        });
    }
    // 单选
    $scope.singleCheck = function (item) {
        if (item.isSel) {
            $scope.checkAll--;
            item.isSel = false;
        } else {
            $scope.checkAll++;
            item.isSel = true;
        }
    }
    /*
    *   遍历选中的对象
    *   list 集合
    *   serIds ref  id集合
    *   serItems ref obj集合不用传null
    */
    $scope.getParamsList = function (list, serIds, serItems) {
        angular.forEach(list, function (data, ind, arr) {//循环全部对象
            if (data.isSel) {// 找到选中的对象
                serIds.push(data.questionnaireId); // 存id
                if (serItems != null)
                    serItems.push(data); // 存obj
            }
        });
    }
    $scope.selName = "全部";
    //loading 加载
    $scope.Loading = false;
    //获取列表
    $scope.getList = function () {
        $scope.Loading = true;
        IS.findQuestionnairePage($scope.params).then(function (response) {
            $scope.Loading = false;
            if (response.errorcode == "0") {
                $scope.List = response.datas;
                $scope.paginationConf.totalItems = response.numCount;
                $scope.paginationConf.currentPage = response.pageIndex;
            } else {
                Msg.error("获取列表失败了呦！");
            }
        }, function errorCallback() {
            $scope.Loading = false;
            console.log("连接服务器失败！");
        });
    }
    //回车事件
    $scope.findListEnter = function (e) {
        var keycode = window.event ? e.keyCode : e.which;
        if (keycode == 13) {
            $scope.getList();
        }
    }
    // 删除
    $scope.delinfo = function (item) {
        var ids = [];
        if (item == null) { // 判断是不是单个的删除
            $scope.getParamsList($scope.List, ids, null);
        } else {// 单独删除
            ids.push(item.questionnaireId);
        }
        // 未选中
        if (Comm.isNull(ids) || ids.length <= 0) {
            Msg.warning("提示", "请选择要删除的数据!");
            return;
        }
        //提示框
        MsgCon.delnoicon().then(function (flag) {   // 删除弹框
            if (flag) {
                $scope.Loading = true;
                // 发送请求
                IS.delQuestionnaire({ qid: ids.join(","), userid: sysUserInfo.getuserinfo().user_ID }).then(function (respose) {
                    $scope.Loading = false;
                    if (respose.errorcode == "0") {
                        Msg.success("操作成功！");
                        $scope.checkAll = 0; // 复选框选中的数量            
                        $scope.getList();  // 重新请求一下数据
                    } else {
                        Msg.error("操作失败！");
                    }
                }, function errorCallback() {
                    console.log("连接服务器失败！");
                    $scope.Loading = false;
                });
            }
        }, function (p) { console.log('error') });
    }
    //配置分页
    $scope.paginationConf = {
        currentPage: 1, //$location.search().currentPage ? $location.search().currentPage :  当前页码
        totalItems: 0,  // 总的记录数
        itemsPerPage: 10, // 每页显示多少条        
        perPageOptions: [10, 20],   // 每页显示记录数        
        onChange: function () {
            $scope.paginationConf.currentPage = $scope.paginationConf.currentPage == 0 ? 1 : $scope.paginationConf.currentPage;
            if ((parseInt($scope.params.pageIndex) != parseInt($scope.paginationConf.currentPage)) || (parseInt($scope.params.pageSize) != parseInt($scope.paginationConf.itemsPerPage))) {
                if ($scope.paginationConf.currentPage == 0) { $scope.paginationConf.currentPage = 1; }
                $scope.params.pageIndex = $scope.paginationConf.currentPage;
                $scope.params.pageSize = $scope.paginationConf.itemsPerPage;
                $scope.getList();
            }
            $scope.checkAll = 0; // 复选框选中的数量     
        }
    };
    $scope.init();
    //打开编辑  tongji
    $scope.openAdd = function (item, index) {

        if (item != undefined && item != null && item != "") {
            $scope.item = item;
        } else {
            var count = 0;
            angular.forEach($scope.List, function (data, ind, arr) {//循环全部对象
                if (data.isSel) {// 找到选中的对象
                    $scope.item = data;
                    count++;
                }
            });
            if (count > 1) {
                Msg.warning("提示", "只能操作一条数据");
                return;
            } else if (count == 0) {
                Msg.warning("提示", "请选择一条数据");
                return;
            }

        }
        var url = "/system/views/wenjuan/wenjuan.html?key=" + $scope.item.questionnaireId
        //统计
        if (index == 1) {
            url = "/system/views/wenjuan/totle_info.html?key=" + $scope.item.questionnaireId + "&xid=0&xtype=0&type=2";
        }
        $scope.openUrl = url;
        $scope.openA();
    }
    /***********************************************************************************************************/
    $scope.previewShow = false;
    //关闭
    $scope.closePreview = function () {
        $scope.previewShow = false;
    }
    //打开
    $scope.addPreview = function (item) {
        if (item != undefined && item != null && item != "") {
            $scope.item = item;
        } else {
            var count = 0;
            angular.forEach($scope.List, function (data, ind, arr) {//循环全部对象
                if (data.isSel) {// 找到选中的对象
                    $scope.item = data;
                    count++;
                }
            });
            if (count > 1) {
                Msg.warning("提示", "只能操作一条数据");
                return;
            } else if (count == 0) {
                Msg.warning("提示", "请选择一条数据");
                return;
            }

        }
        $scope.previewShow = true;
    }

    //打开复制问卷，获取问卷信息
    $scope.copyinfo = function (item) {
        $scope.Loading = true;
        // 发送请求
        IS.findTopicDetails({ qid: item.questionnaireId }).then(function (respose) {
            $scope.Loading = false;
            if (respose.errorcode == "0") {
                wenjuan = respose.data;
                wenjuan.id = new GUID().newGUID();
                wenjuan.title = wenjuan.title + "-副本";
                wenjuan.question = JSON.parse(respose.data.question);
                //组--题--选项
                wenjuan.question.forEach(function (obj, index) {
                    obj.id = new GUID().newGUID();
                    //题
                    if (obj.group != undefined && obj.group.length > 0) {
                        obj.group.forEach(function (gobj, oindex) {
                            gobj.id = new GUID().newGUID();
                            //选项
                            if (obj.qselection != undefined && obj.qselection.length > 0) {
                                obj.qselection.forEach(function (qobj, qindex) {
                                    qobj.id = new GUID().newGUID();
                                });
                            }
                        });
                    }
                });
                if (respose.data.qualified == undefined) {
                    wenjuan.qualified = { min: 0, max: 0 };
                } else {
                    wenjuan.qualified = JSON.parse(respose.data.qualified);
                }
                wenjuan.taglist = JSON.parse(respose.data.taglist);
                wenjuan.result = JSON.parse(respose.data.result);
                $scope.copyQuestionaire(wenjuan);
            } else {
                Msg.error("查询失败！");
            }
        }, function errorCallback() {
            console.log("连接服务器失败！");
            $scope.Loading = false;
        });
    }

    //复制问卷
    $scope.copyQuestionaire = function (item) {
        $scope.Loading = true;
        // 发送请求
        IS.saveOrEditQuestionnaire({ question: JSON.stringify(item), userid: sysUserInfo.getuserinfo().user_ID }).then(function (respose) {
            $scope.Loading = false;
            if (respose.errorcode == "0") {
                Msg.success("复制成功！");
                $scope.getList();
            } else {
                Msg.error("保存失败！");
            }
        }, function errorCallback() {
            console.log("连接服务器失败！");
            $scope.Loading = false;
        });
    }


} ]);
